﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using snat.model;

namespace snat.algorithms.network.TFIDF {
    public class EnronDocument : Document {

        public EnronDocument(Message m, ref Dictionary<String, Term> terms, bool updateTerms) : base(m, ref terms, updateTerms) {
            JObject o = JObject.Parse(m.Data);
            Dictionary<String, int> bagOfWords = JsonConvert.DeserializeObject<Dictionary<String, int>>(o["BOW"].ToString());
            int wordCount = JsonConvert.DeserializeObject<int>(o["WC"].ToString());
            if (bagOfWords.Count == 0) { return; }

            foreach (String word in bagOfWords.Keys) {
                if (!terms.ContainsKey(word) && updateTerms) {
                    terms.Add(word, new Term(word));
                }
                terms[word].AppearenceCount++;
                tf.Add(terms[word], (double)bagOfWords[word] / (double)wordCount);
            }
        }

        public override String DocumentType {
            get {
                return base.DocumentType + "Enron";
            }
        }
        
    }
}
